/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Kate
 */
@Entity
@Table(name = "PRODUCT")
@NamedQueries({
    @NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p"),
    @NamedQuery(name = "Product.findByName", query = "SELECT p FROM Product p WHERE lower(p.name) like lower(:name)"),
    @NamedQuery(name = "Product.findByPrice", query = "SELECT p FROM Product p WHERE p.price = :price"),
    @NamedQuery(name = "Product.findByIdProduct", query = "SELECT p FROM Product p WHERE p.id_product = :id_product"),
    @NamedQuery(name = "Product.findByCategory", query = "SELECT p FROM Product p WHERE p.category = :category"),
    @NamedQuery(name = "Product.findByDescription", query = "SELECT p FROM Product p WHERE p.description = :description"),
    @NamedQuery(name = "Product.findByPicture", query = "SELECT p FROM Product p WHERE p.picture = :picture"),
    @NamedQuery(name = "Product.findByRate", query = "SELECT p FROM Product p WHERE p.rate = :rate"),
    @NamedQuery(name = "Product.findByStatus", query = "SELECT p FROM Product p WHERE p.status = :status"),
    @NamedQuery(name = "Product.findActive", query = "SELECT p FROM Product p WHERE p.status = 0"),
    @NamedQuery(name = "Product.countActive", query = "SELECT count(p) FROM Product p WHERE p.status = 0"),
    @NamedQuery(name = "Product.countActiveByName", query = "SELECT count(p) FROM Product p WHERE p.status = 0 and lower(p.name) like lower(:name) escape '^'"),
    @NamedQuery(name = "Product.countActiveByCategory", query = "SELECT count(p) FROM Product p WHERE p.status = 0 and p.category = :category"),
    @NamedQuery(name = "Product.countActiveByNameCategory", query = "SELECT count(p) FROM Product p WHERE p.status = 0 and lower(p.name) like lower(:name) escape '^' and p.category = :category"),
    @NamedQuery(name = "Product.countAll", query = "SELECT count(p) FROM Product p"),
    @NamedQuery(name = "Product.countAllByName", query = "SELECT count(p) FROM Product p WHERE lower(p.name) like lower(:name) escape '^'"),
    @NamedQuery(name = "Product.countAllByCategory", query = "SELECT count(p) FROM Product p WHERE p.category = :category"),
    @NamedQuery(name = "Product.countAllByNameCategory", query = "SELECT count(p) FROM Product p WHERE lower(p.name) like lower(:name) escape '^' and p.category = :category")
        
    
        
})



public class Product implements Serializable {

    @Id
    @GeneratedValue(generator = "Seq_product")
    @SequenceGenerator(name = "Seq_product", sequenceName = "Seq_product_id_product", allocationSize = 1)
    @Column(name = "id_product")
    int id_product;
    @Column(name = "price")
    @NotNull
    float price;
    @Column(name = "name")
    @NotNull
    String name;
    @JoinColumn(name = "id_category", referencedColumnName = "id_category")
    @ManyToOne(optional = false)
    Category category;
    @Column(name = "description")
    String description;
    @Column(name = "picture")
    String picture;
    @Column(name = "rate")
    @NotNull
    int rate;
    @Column(name = "status")
    @NotNull
    int status;
    private static String popularProductsQuery="select p.id_product,p.name,pl.total_quantity,p.price from "
            + " product p "
            + " join "
            + " (select id_product, sum(count) total_quantity "
            + " from  product_list "
            + " group by id_product "
            + " order by total_quantity desc) pl "
            + " on p.id_product=pl.id_product "
            + " where rownum<? ";
    
    

    public Product() {
    }

    public Product(String name, float price, Category category, String description, int rate, int status) {
        this.name = name;
        this.price = price;
        this.category = category;
        this.description = description;
        this.rate = rate;
        this.status = status;
    }

    public int getId_product() {
        return id_product;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public int getRate() {
        return rate;
    }

    public void setRate(int rate) {
        this.rate = rate;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        // int hash = 0;
        // hash += (id_product != null ? id_product.hashCode() : 0);
        return id_product;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if (this.id_product == other.id_product) {
            return true;
        } else {
            return false;
        }
    }
    
    
     public static String getPopularProductsQuery(){
        return popularProductsQuery;
     }
}
